/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.admin;

import com.sun.org.apache.xml.internal.serializer.utils.StringToIntTable;
import dto.DepartmentDto;
import dto.EmployeeDto;
import dto.RoleDto;
import dto.TableDataModel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import logic.EmployeeLogicImpl;
import logic.ValidateEmployee;

/**
 *
 * @author khangpm
 */
public class AdminEmployee extends javax.swing.JPanel {
    EmployeeDto emp, temp, manager;
    DepartmentDto dept;
    RoleDto role;
    EmployeeLogicImpl logic;
    ArrayList<EmployeeDto> list, listManager;
    TableDataModel model;
    DefaultComboBoxModel modelRole, modelDept, modelManager;
    StringTokenizer token;
    Calendar calendar;
    ValidateEmployee validate;
    
    /**
     * Creates new form AdminEmployee
     */
    public AdminEmployee(EmployeeDto emp) {
        initComponents();
        this.emp = emp;
        logic = new EmployeeLogicImpl();
        list = new ArrayList<EmployeeDto>();
        list = logic.getListEmployee(null, 0);
        listManager = logic.getListManager();
        calendar = Calendar.getInstance();
        validate = new ValidateEmployee();
        setTable();
        setCombobox();
    }
    
    public void setTable(){
        ArrayList colHeader = new ArrayList();
        ArrayList dataTable = new ArrayList();
        ArrayList temp;
        String birthday,day,month,year;
        colHeader.add("EmployeeId");
        colHeader.add("Full Name");
        colHeader.add("Birthday");
        colHeader.add("Gender");
        colHeader.add("Department");
        colHeader.add("Manager");
        colHeader.add("Role");
        for (int i = 0; i < list.size(); i++) {
            temp = new ArrayList();
            temp.add(list.get(i).getEmployeeID());
            temp.add(list.get(i).getEmployeeName());
            birthday = list.get(i).getBirthday().toString();
            token = new StringTokenizer(birthday,"-");
            year = token.nextToken();
            month = token.nextToken();
            day = token.nextToken();
            temp.add(day+"-"+month+"-"+year);
            if(list.get(i).getGender()==0){
                temp.add("Female");
            }else{
                temp.add("Male");
            }
            temp.add(list.get(i).getDepartmentName());
            temp.add(list.get(i).getManagerName());
            temp.add(list.get(i).getRoleName());
            dataTable.add(temp);
        }
        model = new TableDataModel(dataTable, colHeader);
        tblListEmployee.setModel(model);
    }
    
    
    public void setCombobox(){
        ArrayList<RoleDto> allRole;
        ArrayList<DepartmentDto> allDept;
        ArrayList<EmployeeDto> allManager;
        modelRole = new DefaultComboBoxModel();
        modelDept = new DefaultComboBoxModel();
        modelManager = new DefaultComboBoxModel();
        
        allRole = logic.getListRole();
        allDept = logic.getListDepartment();
        allManager = logic.getListManager();
        for (int i = 0; i < allRole.size(); i++) {
            RoleDto temp = allRole.get(i);
            modelRole.addElement(temp.getRoleName());
        }
        cbxRole.setModel(modelRole);

        for (int i = 0; i < allDept.size(); i++) {
            DepartmentDto temp = allDept.get(i);
            modelDept.addElement(temp.getDepartmentName());
        }
        cbxDept.setModel(modelDept);

        modelManager.addElement("No Managerment");
        for (int i = 0; i < allManager.size(); i++) {
            EmployeeDto temp = allManager.get(i);
            modelManager.addElement(temp.getEmployeeName());
        }
        cbxManager.setModel(modelManager);
    }
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListEmployee = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtFullName = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        txtUserName = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        dateChooser = new com.toedter.calendar.JDateChooser();
        psfPassword = new javax.swing.JPasswordField();
        cbxRole = new javax.swing.JComboBox();
        cbxDept = new javax.swing.JComboBox();
        cbxManager = new javax.swing.JComboBox();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        rabMale = new javax.swing.JRadioButton();
        rabFemale = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        txtImage = new javax.swing.JTextField();
        btnImage = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(750, 510));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(750, 510));

        jLabel1.setFont(new java.awt.Font("Vani", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 51));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Employee Manage");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        tblListEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        tblListEmployee.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        tblListEmployee.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListEmployeeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblListEmployee);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setText("Employee Name");

        jLabel3.setText("Brithday");

        jLabel4.setText("Gender");

        jLabel5.setText("Address");

        jLabel6.setText("Email");

        jLabel8.setText("Role");

        jLabel9.setText("Department");

        jLabel10.setText("Manager");

        jLabel11.setText("UserName");

        jLabel12.setText("Password");

        txtFullName.setEditable(false);

        txtEmail.setEditable(false);

        txtUserName.setEditable(false);

        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        txaAddress.setEnabled(false);
        jScrollPane2.setViewportView(txaAddress);

        dateChooser.setEnabled(false);

        psfPassword.setEditable(false);

        cbxRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbxRole.setEnabled(false);

        cbxDept.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbxDept.setEnabled(false);

        cbxManager.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbxManager.setEnabled(false);

        btnAdd.setText("Add New");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        buttonGroup1.add(rabMale);
        rabMale.setText("Male");
        rabMale.setEnabled(false);

        buttonGroup1.add(rabFemale);
        rabFemale.setText("Female");
        rabFemale.setEnabled(false);

        jLabel7.setText("Image");

        txtImage.setEditable(false);

        btnImage.setText("Browse...");
        btnImage.setEnabled(false);
        btnImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rabMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rabFemale))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtImage, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(cbxManager, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbxDept, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(83, 83, 83))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(psfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap())
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbxRole, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(200, 200, 200))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(rabMale)
                            .addComponent(rabFemale)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbxDept, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(cbxManager, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(psfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(34, 34, 34))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(cbxRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnImage))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(171, 171, 171)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        int status = 0;
        if(btnAdd.getText().equals("Add New") && status == 0){
            setEnableFieldTrue();
            resetField();
            btnAdd.setText("Save");
            btnDelete.setEnabled(false);
            btnEdit.setEnabled(false);
            tblListEmployee.setEnabled(false);
            tblListEmployee.setSelectionMode(0);
            setCombobox();
        }else{
            String username,employeeName,password,email,birthday,address,image, manager;
            int gender= -1,deptId,roleId,managerId = 0;
            username = txtUserName.getText();
            employeeName = txtFullName.getText();
            password = String.valueOf(psfPassword.getPassword());
            email = txtEmail.getText();
            address = txaAddress.getText();
            image = txtImage.getText();
            Calendar dayOfBirth = dateChooser.getCalendar();
            int day = dayOfBirth.get(Calendar.DAY_OF_MONTH);
            int month = dayOfBirth.get(Calendar.MONTH);
            int year = dayOfBirth.get(Calendar.YEAR);
            birthday = month+"-"+day+"-"+year;
            if(rabMale.isSelected()){
                gender = 1;
            }else if(rabFemale.isSelected()){
                gender = 0;
            }

            dept = logic.getDepartmentByName(cbxDept.getSelectedItem().toString()) ;
            deptId = dept.getDeptID();

            role = logic.getRoleByName(cbxRole.getSelectedItem().toString()) ;
            roleId = role.getRoleId();

            manager = String.valueOf(cbxManager.getSelectedItem());
            for (int i = 0; i < listManager.size(); i++) {
                EmployeeDto mana = listManager.get(i);
                if(manager.equals(mana.getEmployeeName())){
                    managerId = mana.getEmployeeID();
                    break;
                }
            }
            if(validate.validate(username, employeeName, password, email, birthday, gender, address, deptId, roleId, managerId, image)){
                EmployeeDto add = new EmployeeDto();
                add.setAddress(address);
                add.setBirthday(birthday);
                add.setDeptID(deptId);
                add.setEmailID(email);
                add.setEmployeeName(employeeName);
                add.setGender(gender);
                add.setImage(image);
                add.setManagerID(managerId);
                add.setPassword(password);
                add.setRoleID(roleId);
                add.setUserName(username);
                if(logic.insertEmployee(add)){
                    JOptionPane.showMessageDialog(null, "Add employee successfully", "Successfully", JOptionPane.INFORMATION_MESSAGE);
                    setTable();
                    btnDelete.setEnabled(true);
                    btnEdit.setEnabled(true);
                    setEnableFieldFalse();
                    btnAdd.setText("Add New");
                    tblListEmployee.setEnabled(false);
                }else{
                    JOptionPane.showMessageDialog(null, "Add employee false", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }else{
                status = 1;
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void tblListEmployeeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListEmployeeMouseClicked
        // TODO add your handling code here:
        if(tblListEmployee.getSelectedRow() == -1){
        }else{
            temp = logic.getEmployeeByID(Integer.parseInt(tblListEmployee.getValueAt(tblListEmployee.getSelectedRow(),0).toString()));

            int year, month, day;
            token = new StringTokenizer(tblListEmployee.getValueAt(tblListEmployee.getSelectedRow(),2).toString(),"-");
            day = Integer.parseInt(token.nextToken());
            month = Integer.parseInt(token.nextToken());
            year = Integer.parseInt(token.nextToken());
            calendar.set(year, month-1,day);
            dateChooser.setCalendar(calendar);


            txtFullName.setText(temp.getEmployeeName());
            txtUserName.setText(temp.getUserName());
            txtEmail.setText(temp.getEmailID());
            txaAddress.setText(temp.getAddress());
            psfPassword.setText(temp.getPassword());
            txtImage.setText(temp.getImage());

            if(tblListEmployee.getValueAt(tblListEmployee.getSelectedRow(),3).toString().equals("Male")){
                rabMale.setSelected(true);
            }else{
                rabFemale.setSelected(true);
            }

            dept = logic.getDeparment(temp.getDeptID());
            for (int i = 0; i < cbxDept.getItemCount(); i++) {
                if(cbxDept.getItemAt(i).toString().equals(dept.getDepartmentName())){
                    cbxDept.setSelectedIndex(i);
                    break;
                }
            }

            role = logic.getRole(temp.getRoleID());
            for (int i = 0; i < cbxRole.getItemCount(); i++) {
                if(cbxRole.getItemAt(i).toString().equals(role.getRoleName())){
                    cbxRole.setSelectedIndex(i);
                    break;
                }
            }

            for (int i = 0; i < modelManager.getSize(); i++) {
                if(modelManager.getElementAt(i).toString().equals(tblListEmployee.getValueAt(tblListEmployee.getSelectedRow(),5).toString())){
                    cbxManager.setSelectedIndex(i);
                    break;
                }
            }
        }
    }//GEN-LAST:event_tblListEmployeeMouseClicked

    private void btnImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImageActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_btnImageActionPerformed
    
    public void setEnableFieldTrue(){
        txaAddress.setEnabled(true);
        txtEmail.setEditable(true);
        txtFullName.setEditable(true);
        txtImage.setEditable(true);
        txtUserName.setEditable(true);
        psfPassword.setEditable(true);
        btnImage.setEnabled(true);
        cbxDept.setEnabled(true);
        cbxManager.setEnabled(true);
        cbxRole.setEnabled(true);
        rabFemale.setEnabled(true);
        rabMale.setEnabled(true);
        dateChooser.setEnabled(true);
    }
    
     public void setEnableFieldFalse(){
        txaAddress.setEnabled(false);
        txtEmail.setEditable(false);
        txtFullName.setEditable(false);
        txtImage.setEditable(false);
        txtUserName.setEditable(false);
        psfPassword.setEditable(false);
        btnImage.setEnabled(false);
        cbxDept.setEnabled(false);
        cbxManager.setEnabled(false);
        cbxRole.setEnabled(false);
        rabFemale.setEnabled(false);
        rabMale.setEnabled(false);
        dateChooser.setEnabled(false);
    }
    
    public void resetField(){
        txaAddress.setText("");
        txtEmail.setText("");
        txtFullName.setText("");
        txtImage.setText("");
        txtUserName.setText("");
        psfPassword.setText("");
        cbxDept.setSelectedIndex(0);
        cbxManager.setSelectedIndex(0);
        cbxRole.setSelectedIndex(0);
        rabFemale.setSelected(false);
        rabMale.setSelected(false);
        dateChooser.setCalendar(null);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnImage;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbxDept;
    private javax.swing.JComboBox cbxManager;
    private javax.swing.JComboBox cbxRole;
    private com.toedter.calendar.JDateChooser dateChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPasswordField psfPassword;
    private javax.swing.JRadioButton rabFemale;
    private javax.swing.JRadioButton rabMale;
    private javax.swing.JTable tblListEmployee;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullName;
    private javax.swing.JTextField txtImage;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
